﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using QLChoDiaOc.Process;
using System.Data;

namespace QLChoDiaOc.Forms
{
    /// <summary>
    /// Interaction logic for FormThongKeDoanhThuTungThang.xaml
    /// </summary>
    public partial class FormThongKeDoanhThuTungThang : Page
    {
        SelectInfo _selectInfo = new SelectInfo();
        ShowInfo _showInfo = new ShowInfo();
        ListInfo _listInfo = new ListInfo();
        OtherHandle _otherHandle = new OtherHandle();
        static bool _bSelectData = false;
        static DataTable _dataTable = new DataTable();

        public FormThongKeDoanhThuTungThang()
        {
            InitializeComponent();
            AddComboBox();
        }

        void AddComboBox()
        {
            cmbNamBC.Items.Clear();
            cmbThangBC.Items.Clear();
            for (int i = 1; i <= 12; i++)
                cmbThangBC.Items.Add(i);
            for (int i = 2010; i <= DateTime.Now.Year; i++)
                cmbNamBC.Items.Add(i);
        }

        private void btnBaoCaoPhieuThu_Click(object sender, RoutedEventArgs e)
        {
            int _thang = int.Parse(cmbThangBC.SelectedItem.ToString());
            int _nam = int.Parse(cmbNamBC.SelectedItem.ToString());
            _showInfo.ShowBCPhieuThu(dataPhieuThu, _thang, _nam, ref _dataTable);

            int _tongtien = 0;
            for (int i = 0; i < _listInfo.ListPhieuThuTungThang(_thang, _nam).Count; i++)
                _tongtien += int.Parse(_listInfo.ListPhieuThuTungThang(_thang, _nam)[i].SoTien.ToString());
            TongTienTextBox.Text = _tongtien.ToString();
            btnReport.IsEnabled = true;
        }

        private void dataPhieuThu_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!_bSelectData)
            {
                try
                {
                    var item = dataPhieuThu.SelectedItem as PHIEUTHU;

                    maPhieuThuTextBox.Text = item.MaPhieuThu;
                    soTienThuTextBox.Text = item.SoTien.ToString();
                    ngayHetHanDatePicker.SelectedDate = item.NgayHetHan;
                    ngayThuDatePicker.SelectedDate = item.NgayThu;
                    maDangKyTextBox.Text = item.MaDangKy;

                    PHIEUDANGKY _phieuDangKy = new PHIEUDANGKY();
                    _phieuDangKy = _selectInfo.SelectPhieuDangKy(maDangKyTextBox.Text);

                    DIAOC _diaoc = new DIAOC();
                    _diaoc = _selectInfo.SelectDiaOc(_phieuDangKy.MaDiaOc);

                    KHACHHANG _khachhang = new KHACHHANG();
                    _khachhang = _selectInfo.SelectKhachHang(_diaoc.MaKH);

                    maKHTextBox.Text = _khachhang.MaKH;
                    hoTenKHTextBox.Text = _khachhang.HoTen;
                    maDiaOcTextBox.Text = _diaoc.MaDiaOc;
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message, "Thông Báo");
                }
            }
            else
            {
                _bSelectData = false;
                _showInfo = new ShowInfo();
                int _thang = int.Parse(cmbThangBC.SelectedItem.ToString());
                int _nam = int.Parse(cmbNamBC.SelectedItem.ToString());
                _showInfo.ShowBCPhieuThu(dataPhieuThu, _thang, _nam);
            }
        }

        private void btnReport_Click(object sender, RoutedEventArgs e)
        {
            DataSet _dataSet = new DataSet();

            _dataSet = _otherHandle.ConvertDataTableToDataSet(_dataTable);
            Export.exportExcel("Thống Kê Doanh Thu", _dataSet);
        }
    }
}
